package lib.bone.admin.controller.nepu;

import cn.hutool.core.lang.Validator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.access.module.oss.event.EntityRemoveEvent;
import lib.bone.nepudata.service.ResearchServiceCtl;
import lib.bone.admin.anno.WebLog;
import lib.bone.admin.constant.MethodTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("nepu")
@Api(tags = "实体管理")
public class NepuEntityController {
    @Autowired
    ResearchServiceCtl serviceCtl;

    @Autowired(required = false)
    EntityRemoveEvent removeEvent;

    @GetMapping("entity")
    @ApiOperation(value = "获取实体信息")
    public ResponseDTO<Object> getEntity(@RequestParam("entityId") String entityId,@RequestParam("type") String type){
        Optional<Object> entityOp = serviceCtl.getById(entityId,type);
        return entityOp.map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND));
    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.DELETE)
    @DeleteMapping("entity")
    @ApiOperation(value = "删除实体信息")
    public ResponseDTO deleteEntity(@RequestParam("entityId") String entityId,@RequestParam("type") String type){
        serviceCtl.deleteById(entityId,type);
        if (Validator.isNotEmpty(removeEvent)) removeEvent.removeLinkFiles(entityId);
        return ResponseDTO.succMsg("删除成功");
    }
}
